package com.qk.management.service.impl;

import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author diva
 * @date 2025/10/8 19:59
 * @ProjectName CourseServiceImpl
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public PageResult<Course> page(Integer subject, String name, Integer price, Integer target, String description, Integer page, Integer pageSize) {
        Integer total=courseMapper.count(subject,name,price,target,description);
        Integer offset = (page-1)*pageSize;
        List<Course> rows= courseMapper.selectByPage(subject,name,price,target,description,offset,pageSize);
        return PageResult.<Course>builder()
                .total(total)
                .rows(rows)
                .build();
    }
    @Override
    public void addCourse(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insert(course);
    }
    @Override
    public Course getCourseById(Integer id) {
        return courseMapper.selectById(id);
    }
    @Override
    public List<Course> selectAll() {
        return courseMapper.selectAll();
    }
    @Override
    public void deleteCourse(Integer id) {
        courseMapper.deleteById(id);
    }
    @Override
    public void update(Course course) {
        courseMapper.update(course);
    }

    @Override
    public List<Course> getCourseBySubject(Integer subject) {
        return courseMapper.selectBySubject(subject);
    }
}
